package com.lxh.weekendmvc.web.controller;


import com.github.pagehelper.PageInfo;
import com.lxh.weekendmvc.entity.Emp;
import com.lxh.weekendmvc.services.EmpServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
@RequestMapping("/emp")
public class EmpController {

    @Autowired
    private EmpServices empServices;

    @RequestMapping("/getEmpByPage")
    public String getEmpByPage(@RequestParam Map<String,String> emps, Model model){
        //使用map集合以键值对的形式打包前端传入的所有的数值,取前端数值是只需按key值寻找。
        //key的值即是前端标签的name值
        //value的值即是前端表单的value值
        PageInfo<Emp> info = empServices.getEmpByPage(emps);
        model.addAttribute("info",info);
        return "index";
    }

    @RequestMapping("/addEmp")
    public String addEmp(Emp emp,Model model){
        if(empServices.addEmp(emp)){
            model.addAttribute("msg","新增成功");
        }else {
            model.addAttribute("msg","新增失败");
        }
        return "message";
    }

    @RequestMapping("/checkName")
    @ResponseBody
    public String checkName(String ename){
        if(empServices.checkName(ename)){
            //查询到了ename信息，说明ename存在，既用户名不可用
            return "0";
        }else {
            //没有查到ename相关信息说明ename不存在，既用户名可用
            return "1";
        }

    }


    @RequestMapping("/getEmpByEno")
    public String getEmpByEno(Integer eno,Model model){
        model.addAttribute("e",empServices.getEmpByEno(eno));
        return "updateEmp";
    }

    @RequestMapping("/updateEmp")
    public String updateEmp(Emp emp,Model model){
        if(empServices.updateEmp(emp)){
            model.addAttribute("msg","修改成功");
        }else {
            model.addAttribute("msg","修改失败");
        }
        return "message";
    }



    @RequestMapping("/del")
    @ResponseBody
    public String delEmp(Integer eno){
        if(empServices.delEmp(eno)){
            return "1";
        }else {
            return "0";
        }

    }


}
